/* 
    import { defineStore } from 'pinia'
import { nanoid } from 'nanoid'
import axios from 'axios'
export const useLoveTalkStore = defineStore('talk',{
    state(){
        return {
            talkList:JSON.parse(localStorage.getItem('talkList') as string) || [
                {id:'lsjfglkjh01',title:'张三'},
                {id:'lsjfglkjh02',title:'李四'},
                {id:'lsjfglkjh03',title:'王五'}
            ]
        }
    },
    actions:{
        async getAtalk(){
            // 发请求 下面这行的写法是:连续解构赋值+重命名
            let {data:{content:title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
            console.log(title)
            // 把请求回来的字符串,包装成对象
            let obj = {id:nanoid(),title}
            this.$state.talkList.unshift(obj)
        }
    }
})

*/


import { defineStore } from 'pinia'
import { reactive } from 'vue'
import { nanoid } from 'nanoid'
import axios from 'axios'
export const useLoveTalkStore = defineStore('talk',()=>{
    const talkList = reactive(
        JSON.parse(localStorage.getItem('talkList') as string) || []
    )

    async function getAtalk(){
        // 发请求 下面这行的写法是:连续解构赋值+重命名
        let {data:{content:title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
        console.log(title)
        // 把请求回来的字符串,包装成对象
        let obj = {id:nanoid(),title}
        talkList.unshift(obj)
    }

    return {getAtalk,talkList}
})